package com.Imp;

import com.entity.Cart;
import com.mapper.CartMapper;
import com.util.MyBatisUtil;
import org.apache.ibatis.session.SqlSession;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class CartMapperImpl implements CartMapper {

    public void addToCart(Cart cart) {
        try (SqlSession sqlSession = MyBatisUtil.openSqlSession()) {
            sqlSession.insert("com.mapper.CartMapper.addToCart",cart);
            sqlSession.commit();  // 提交事务
        }
    }
    public void updateCart(Cart cart) {
        try (SqlSession sqlSession = MyBatisUtil.openSqlSession()) {
            sqlSession.update("com.mapper.CartMapper.updateCart", cart);
            sqlSession.commit();  // 提交事务
        }
    }

    public void deleteBystoreId(String storeId){
        try (SqlSession sqlSession = MyBatisUtil.openSqlSession()) {
            sqlSession.update("com.mapper.CartMapper.deleteBystoreId", storeId);
            sqlSession.commit();  // 提交事务
        }
    }

    // 实现从购物车删除商品的数据库操作
    public void deleteFromCart(String storeId, String customerId , String cuisineId) {
        try (SqlSession sqlSession = MyBatisUtil.openSqlSession()) {
            Map<String, Object> params = new HashMap<>();
            params.put("storeId", storeId);
            params.put("customerId", customerId);
            params.put("cuisineId", cuisineId);
            sqlSession.delete("com.mapper.CartMapper.deleteFromCart", params);
            sqlSession.commit();  // 提交事务
        }
    }

    // 实现从购物车删除商品的数据库操作
    public void deleteByStoreIdAndCustomerId(String storeId, String customerId ) {
        try (SqlSession sqlSession = MyBatisUtil.openSqlSession()) {
            Map<String, Object> params = new HashMap<>();
            params.put("storeId", storeId);
            params.put("customerId", customerId);
            sqlSession.delete("com.mapper.CartMapper.deleteByStoreIdAndCustomerId", params);
            sqlSession.commit();  // 提交事务
        }
    }

    public List<Cart> getCartByCustomerId(String customerId) {
        try (SqlSession sqlSession = MyBatisUtil.openSqlSession()){
            Map<String, Object> param = new HashMap<>();
            param.put("customerId", customerId);
            return sqlSession.selectList("com.mapper.CartMapper.getCartByCustomerId", param);
        }
    }

    public List<Cart> getCartByStoreId(String storeId, String customerId) {
        try (SqlSession sqlSession = MyBatisUtil.openSqlSession()){
            Map<String, Object> params = new HashMap<>();
            params.put("storeId", storeId);
            params.put("customerId", customerId);
            return sqlSession.selectList("com.mapper.CartMapper.getCartByStoreId", params);
        }
    }

    public Cart getCartByCuisineId(String storeId, String customerId , String cuisineId) {
        try (SqlSession sqlSession = MyBatisUtil.openSqlSession()){
            Map<String, Object> params = new HashMap<>();
            params.put("storeId", storeId);
            params.put("customerId", customerId);
            params.put("cuisineId", cuisineId);
            return sqlSession.selectOne("com.mapper.CartMapper.getCartByCuisineId", params);
        }
    }


}
